import { VuexModule, Module, Action, getModule, Mutation } from 'vuex-module-decorators'
import { router_CONTRACT, routerAbi, AIBOT_TOKEN, USDT_TOKEN } from '@/contract'
import { ethers } from "ethers";
import { toWei } from "@/utils/format";

import store from '@/store'

interface RouterState {
}

@Module({ dynamic: true, store, name: 'Router', namespaced: true })
class Router extends VuexModule implements RouterState {

    @Action({ rawError: true })
    public getAmountsOut({ amount, fromTOKEN, toTOKEN }: { amount: string | number, fromTOKEN: string | number, toTOKEN: string }): Promise<any> {
        const contract = new store.getters.web3.eth.Contract(routerAbi, router_CONTRACT);
        return contract.methods.getAmountsOut(toWei(amount), [fromTOKEN, toTOKEN]).call()
    }

    @Action({ rawError: true })
    public async swapExactTokensForTokensSupportingFeeOnTransferTokens({ amountIn, amountOutMin, fromTOKEN, toTOKEN }: { amountIn: string | number, amountOutMin: string | number, fromTOKEN: string, toTOKEN: string }): Promise<any> {
        const ethersProvider = new ethers.providers.Web3Provider(
            window.ethereum,
            "any"
        );
        const contract = new ethers.Contract(
            router_CONTRACT,
            routerAbi,
            ethersProvider.getSigner()
        );
        console.log(
            amountIn,
            amountOutMin,
            [fromTOKEN, toTOKEN],
            store.getters.userAddr,
            (Number((new Date().getTime() + 20 * 60000).toString().substr(0, 10)) + 1200)
        )
        const result = await contract.swapExactTokensForTokensSupportingFeeOnTransferTokens(
            amountIn,
            amountOutMin,
            [fromTOKEN, toTOKEN],
            store.getters.userAddr,
            (Number((new Date().getTime() + 20 * 60000).toString().substr(0, 10)) + 1200),
        );
        return result.wait()
    }

}

export const RouterModule = getModule(Router)